package com.zd.springcloud.tx;

import com.mysql.cj.jdbc.JdbcConnection;
import com.mysql.cj.jdbc.MysqlXAConnection;
import com.zd.springcloud.tx.mapper.AdminMapper;
import com.zd.springcloud.tx.mapper.UserMapper;
import com.zd.springcloud.tx.util.FactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.sql.XAConnection;
import javax.transaction.xa.XAResource;
import java.sql.Connection;

/**
 * mybatis 框架整合XA遇到的问题：模拟原生API的方式太麻烦了不太优雅了。
 */
public class MybatisShiWu {
    //王五给赵六转钱
    public static void main(String[] args) throws Exception{

        //王五减钱
        SqlSessionFactory aaaSqlSessionFactory = FactoryUtil.getAAASqlSessionFactory();
        SqlSession aaaSqlSession = aaaSqlSessionFactory.openSession(true);
        aaaSqlSession.getMapper(AdminMapper.class).reduceMoney(100.00);
        /**
         * 模拟原生API的方式太麻烦了
         */
//       Connection connection = aaaSqlSession.getConnection();
        //是否打印日志
//        XAConnection aaaXAConnection = new MysqlXAConnection((JdbcConnection) connection,false);
//        XAResource aaaXAResource = aaaXAConnection.getXAResource();
//        aaaXAResource.start("");
//        aaaXAConnection.getConnection().prepareStatement("sq").execute();
//        aaaXAResource.end("",XAResource.TMSUCCESS);
//        aaaXAResource.prepare("");
//        aaaXAResource.commit();



        //赵六加钱
        SqlSessionFactory bbbSqlSessionFactory = FactoryUtil.getBBBSqlSessionFactory();
        SqlSession bbbSqlSession = bbbSqlSessionFactory.openSession(true);
        bbbSqlSession.getMapper(UserMapper.class).addMoney(100.00);




        aaaSqlSession.close();
        bbbSqlSession.close();
    }
}
